<?php
namespace Admin\Controller;
use Think\Controller;
class ShowController extends Controller {

      public function index(){
          if(IS_POST){
              $data = I('post.');
              //dump($_FILES);die;
              $data['id']=1;
              $data2 = M('bg')->find(1);
              //图片上传
              if($_FILES['bgurl']){
                  $upload = new \Think\Upload();
                  $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
                  $upload->rootPath = './Public/';
                  $upload->savePath = 'uploads/';
                  $info = $upload->uploadOne($_FILES['bgurl']);
                  $data['bgurl'] = $info['savepath'].$info['savename'];
              }
              if (!$data['bgurl']) {
                  $data['bgurl'] =$data2['bgurl'];
              }
              $res = M('bg')->save($data);
              //判断插入成功
              if($res){
                  $this->success('修改成功',U('Show/index'));
              }else{
                  $this->error('修改失败');
              }

          }else{
              //关键字
              $data = M('bg')->find(1);
              //发送数据并显示
              $this->assign('data',$data);
              $this->display();
          }
      }

      public function video(){
          if(IS_POST){
              $data = I('post.');
              //dump($_FILES);die;
              $data['id']=1;
              $data2 = M('video')->find(1);
              //图片上传
              if($_FILES['videoimgurl']){
                  $upload = new \Think\Upload();
                  $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
                  $upload->rootPath = './Public/';
                  $upload->savePath = 'uploads/';
                  $info = $upload->uploadOne($_FILES['videoimgurl']);
                  $data['videoimgurl'] = $info['savepath'].$info['savename'];
              }
              if (!$data['videoimgurl']) {
                  $data['videoimgurl'] =$data2['videoimgurl'];
              }
              $res = M('video')->save($data);
              //判断插入成功
              if($res){
                  $this->success('修改成功',U('Show/video'));
              }else{
                  $this->error('修改失败');
              }

          }else{
              //关键字
              $data = M('video')->find(1);
              //发送数据并显示
              $this->assign('data',$data);
              $this->display();
          }
      }

    public function navigation(){
        $data =M('banner')->select();
        $this->assign('data',$data);
        $this->display();
    }

    public function navigationedit(){
        if(IS_POST){
            $data = I('post.');
            //图片上传
            $id=$data['id'];

            //dump($data);exit();
            //验证数据
            $res = M('banner')->save($data);
            //判断插入成功
            if($res){
                $this->success('修改成功',U('Show/navigation'));
            }else{
                $this->error('修改失败');
            }
        }else{
            $id=$_GET['id'];
            $data =M('banner')->find($id);
            $this->assign('data',$data);
            $this->display();
        }
    }

    //logo
    public function logo(){
        if(IS_POST){
            $data = I('post.');
            //dump($_FILES);die;
            $data['id']=1;
            $data2 = M('logo')->find(1);
            //图片上传
            if($_FILES['logo']){
                $upload = new \Think\Upload();
                $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
                $upload->rootPath = './Public/';
                $upload->savePath = 'uploads/';
                $info = $upload->uploadOne($_FILES['logo']);
                $data['logo'] = $info['savepath'].$info['savename'];
            }



            if (!$data['logo']) {
                $data['logo'] =$data2['logo'];
            }
            //验证数据
            $res = M('logo')->save($data);
            //判断插入成功
            if($res){
                $this->success('修改成功',U('Show/logo'));
            }else{
                $this->error('修改失败');
            }

        }else{
            //关键字
            $data = M('logo')->find(1);
            //发送数据并显示
            $this->assign('data',$data);
            $this->display();
        }
    }


    //删除操作
    public function del(){
        //删除数据
        $res = M('article')->delete(I('id'));
        //判断对错
        if($res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
    public function edit(){
        if(IS_POST){
            $data = I('post.');
            //图片上传
            if($_FILES['file']){
                $upload = new \Think\Upload();
                $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
                $upload->rootPath = './Public/';
                $upload->savePath = 'uploads/';
                $info = $upload->uploadOne($_FILES['file']);
                $data['img'] = $info['savepath'].$info['savename'];
            }
            $data['content']=html_entity_decode($data['content']);
            $data['add_time']=time();
            //dump($data);exit();
            //验证数据
            $res = M('article')->save($data);
                //判断插入成功
            if($res){
                $this->success('修改成功',U('Show/index'));
            }else{
                $this->error('修改失败');
            }

        }else{
            $id = I('id');
            //关键字
            $data = M('article')->find($id);
            //发送数据并显示
            $this->assign('data',$data);
            $this->display();
        }
    }


}